package com.artisan.common.utils.wxpay;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by xierongxian on 2016/4/11.
 * 字符串操作类
 */
public class StringUtils {

    /**
     * 随机生成6位数字符
     * @param length
     * @return
     */
    public static String getCipher(int length){
        String nice="";
        Random random=new Random();
        for(int i=0;i<length;i++)
        {
            String charOrNum=random.nextInt(2)%2==0?"char":"num";
            if("char".equalsIgnoreCase(charOrNum))
            {
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; //取得大写字母还是小写字母
                nice += (char) (choice + random.nextInt(26));
            }
            else if("num".equalsIgnoreCase(charOrNum))
            {
                nice += String.valueOf(random.nextInt(10));
            }
        }
        return nice;
    }

    /**
     * xierongxian
     * 获取已当前日期为开始后多少天的日期
     * 参数说明 days==多少天，formatString返回的日期格式如（yyyy-MM-dd）
     * */
    public static String getDateByDays(int days,String formatString){
        Date date = new Date();
        System.out.println((new SimpleDateFormat(formatString)).format(date));
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return (new SimpleDateFormat(formatString)).format(cal.getTime());
    }

    /**
     * 判断字符创是否为空
     * */
    public static boolean isEmpty(String str){
        if(null!=str&&!"".equals(str)&&!"null".equals(str)){
            return false;
        }else{
            return true;
        }


    }

    /**
     * 验证手机号码格式是否正确
     * @param phone
     * @return
     */
    public static boolean validateMobile(String phone){
        Pattern pattern = Pattern.compile("^1(3|4|5|7|8)\\d{9}$");
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }

    /**
     * 验证邮件格式是否正确
     * @param email
     * @return
     */
    public static boolean validateEmail(String email){
        Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    public static void main(String args[])throws  Exception{

//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
//
//        System.out.print( simpleDateFormat.parse("2016-04-19"));
        String sendTarget = "13512345678";
        String sendMode = "2";
        System.out.print(sendMode.equals("1")?StringUtils.validateEmail(sendTarget):StringUtils.validateMobile(sendTarget));
    }





}
